package com.vf.admin.client.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.dao.AppFeedbackTypeDao;
import com.vf.admin.client.service.*;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.miaodong.controller.AfterGiftController;
import com.vf.admin.miaodong.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.vf.admin.client.common.ClientConstants.client_toke;
import static com.vf.admin.common.Constants.dongtai_length;
import static com.vf.admin.utils.StringEx.isNull;

/**
 * 举报控制器管理
 */
@Controller
@RequestMapping("/app/feedback")
public class AppFeedbackController extends ClientBaseController {

    @Autowired
    AppFeedbackRecordService  appFeedbackRecordService;
    @Autowired
    AppFeedbackTypeDao appFeedbackTypeDao;
    @Autowired
    AppUserService appUserService;
    @Autowired
    AppDynamicService appDynamicService;
    @Autowired
    AppRoomService appRoomService;
    @Autowired
    private AfterGiftController afterGiftController;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    private AppFamilyService appFamilyService;

    /**
     * 获取举报类型
     */
    @RequestMapping("/list")
    @ResponseBody
    public Result list() {
        List<AppFeedbackType> list = appFeedbackTypeDao.list(null);
        Map<String,Object> mm = new HashMap<>(16);
        mm.put("list",list);
        return sucessJSON(mm);
    }

    /**
     * 举报反馈提交
     * @param bjbid 被举报举报对象的id  动态举报传动态id  房间举报传房间id  用户举报传用户id 家族举报传家族id
     * @param leixing 举报类型 1、动态  2、房间  3用户  4家族
     * @param feedbacktype 举报标题
     * @param img 举报截图
     * @param message 举报原因
     * @return
     */
    @RequestMapping("/addFeedback")
    @ResponseBody
    public Result reportFeedbackSubmission(HttpServletRequest request, Integer bjbid, Integer leixing, String feedbacktype, MultipartFile[] img, String message){
        if (isNull(bjbid)) {
            return errorJSON("请选择举报对象");
        }
        if (isNull(leixing)) {
            return errorJSON("举报类型错误");
        }
        if (isNull(feedbacktype)) {
            return errorJSON("举报标题不能为空");
        }
        if (isNull(message)) {
            return errorJSON("举报原因不能为空");
        }
        if (message.toLowerCase().contains("script")) {
            return this.errorJSON("请不要非法攻击");
        }
        if (message.trim().length() > dongtai_length) {
            return this.errorJSON("举报信息内容长度超出");
        }
        Integer userId = (Integer)redisUtil.get(RedisKeys.userToken + request.getHeader(client_toke));
        //获取当前登录用户的user对象
        AppUser user =appUserService.get(userId);
        if(leixing>3) return errorCLJSON("举报类型错误");
        Integer bjUser=0;
        String bjbPic="";
        String bjbPhone="";
        String bjbNick="";
        String nrbiaoti="";
        if(leixing==1){
            AppDynamic dynamic=appDynamicService.getByid(bjbid);
            if(org.springframework.util.StringUtils.isEmpty(dynamic)){
                return errorCLJSON("动态不存在") ;
            }
            bjUser=dynamic.getUserid();
            bjbPic=dynamic.getPic();
            bjbPhone=dynamic.getPhone();
            bjbNick=dynamic.getNick();
            nrbiaoti=dynamic.getMessage();
        }else if(leixing==3){
            AppUser appUser=appUserService.get(bjbid);
            if(org.springframework.util.StringUtils.isEmpty(appUser)){
                return errorCLJSON("用户不存在") ;
            }
            bjUser=appUser.getId();
            bjbPic=appUser.getPic();
            bjbPhone=appUser.getLoginname();
            bjbNick=appUser.getNick();
            nrbiaoti=appUser.getNick();
        }else if(leixing == 4){
            AppFamily appFamily = appFamilyService.findbyId(bjbid);
            if(StringUtils.isEmpty(appFamily)){
                return errorCLJSON("家族不存在");
            }
            bjUser = appFamily.getId();
            bjbPic = appFamily.getPic();
            bjbPhone = appFamily.getFamilyid().toString();
            bjbNick = appFamily.getFamilyname();
            nrbiaoti = appFamily.getFamilyname();
        }
        String images = "";
        //判断是否上传动态图片
        if (img != null && img.length > 0) {
            //上传：循环获取file数组中得文件
            StringBuffer stringBuffer = new StringBuffer();
            for (int i = 0; i < img.length; i++) {
                MultipartFile files = img[i];
                if (!files.isEmpty()) {
                    //判断图片大小
                    if (files.getSize() <= 1073741824) {
                        //上传阿里云oss
                        String ossUrl = appUserService.aliyunOssupload(files);
//                        Result result = afterGiftController.uploadLiveImg(files);
                        /*Map<String,Object> map = JSONObject.parseObject(JSON.toJSONString(result.getData()));
                        String ossUrl = map.get("name").toString();*/
                        if (isNull(ossUrl)) {
                            return this.errorJSON("图片上传失败");
                        }
                        //拼接
                        stringBuffer.append(ossUrl + ",");
                    } else {
                        return this.errorJSON("图片大小超出限制");
                    }
                }
            }
            //保存图片拼接路径
            images = stringBuffer.toString();
        }
        AppFeedback feedback=new AppFeedback();
        feedback.setUserid(user.getId());
        feedback.setPic(user.getPic());
        feedback.setPhone(user.getLoginname());
        feedback.setNick(user.getNick());
        feedback.setBjbuserid(bjUser);
        feedback.setBjbnick(bjbNick);
        feedback.setBjbphone(bjbPhone);
        feedback.setBjbpic(bjbPic);
        feedback.setLeixing(leixing);
        feedback.setBjbid(bjbid);
        feedback.setNrbiaoti(nrbiaoti);
        feedback.setImg(images);
        feedback.setMessage(message);
        feedback.setCreatetime(new Date());
        feedback.setFeedbacktype(feedbacktype);
        feedback.setState(1);
        appFeedbackRecordService.save(feedback);
        return sucessJSON("成功");
    }
}
